#include "VSGeometryNode.h"
#include "VSGraphicInclude.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSGeometryNode,VSNode)
BEGIN_ADD_PROPERTY(VSGeometryNode,VSNode)
REGISTER_PROPERTY(m_pMorphSet,MorphSet,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSGeometryNode)
IMPLEMENT_INITIAL_END
VSGeometryNode::VSGeometryNode()
{
}
VSGeometryNode::~VSGeometryNode()
{

}
unsigned int VSGeometryNode::DiskUse()const
{
	unsigned int Use = VSNode::DiskUse();

	Use += sizeof(VSMorphSet *);
	return Use;
}
bool VSGeometryNode::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSNode::Save(rStream,iSaveUse))
		return 0;


// 	VSMorphSet * pMorph = m_pMorphSet;
// 	if(!rStream.Write(&pMorph,sizeof(VSMorphSet *)))
// 		return 0;
	if(!rStream.WriteObjectPtr(m_pMorphSet))
		return 0;
	iSaveUse += sizeof(VSMorphSet *);



	return 1;
}
bool VSGeometryNode::Load(VSStream & rStream,unsigned int &iSaveUse)
{
	if(!VSNode::Load(rStream,iSaveUse))
		return 0;

// 	VSMorphSet * pMorph = NULL;
// 	if(!rStream.Read(&pMorph,sizeof(VSMorphSet *)))
// 		return 0;
// 	AddLink(pMorph);
	if(!rStream.ReadObjectPtr(m_pMorphSet))
		return 0;
	iSaveUse += sizeof(VSMorphSet *);

	return 1;
}
bool VSGeometryNode::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSGeometryNode* Temp = DynamicCast<VSGeometryNode>(pObject); 
	if(!Temp)
		return 0;
	if(VSNode::Clone(pObject,uiType))
	{
		m_pMorphSet = Temp->m_pMorphSet;
		return 1;
	}
	else
	{
		return 0;

	}

}
bool VSGeometryNode::Link(VSStream & rStream)
{
	if(!VSNode::Link(rStream))
		return 0;

	//m_pMorphSet = (VSMorphSet *)rStream.GetMapValue(GetNextLink());
	rStream.LinkObjectPtr(m_pMorphSet);

	return 1;
}
bool VSGeometryNode::Register(VSStream & rStream)const
{
	if (rStream.IsObjectRegister(this))
	{
		return 1;
	}
	if(!VSNode::Register(rStream))
		return 0;

	if(m_pMorphSet)
	{
		if(!m_pMorphSet->Register(rStream))
			return 0;
	}


	return 1;
}
VSGeometry * VSGeometryNode::GetGeometry(unsigned int i)
{
	if (i >= m_pChild.GetNum())
	{
		return NULL;
	}
	else
	{
		VSGeometry * pGeometry= DynamicCast<VSGeometry>(m_pChild[i]);
		return pGeometry;
	}
}